// Node.js和vue3实现Gitee码云OAuth2第三方登录
var express = require('express');
var router = express.Router();
const axios = require('axios'); // 导入 Axios 模块，用于发起 HTTP 请求



// gitee配置信息
const giteeConfig = {
  // 替换为你的回调地址
  redirect_uri: 'http://127.0.0.1:9090/pages/giteeLogin',
  // 替换为你的 client_id
  client_id: 'f1f83dec6b5438508baaeb23bc6d6b7510b2db6f618d290bc4fb936e9e7ff413',
  // 替换为你的 client_secret
  client_secret: '8f5684aab67849fe9a17f24714d4bf210af551c0809a08bbcb92ca4260d13ddd',
}

// gitee登录
router.post('/api/gitee/login', async (req, res) => {
  // 1、校验必填参数
  if (!req.body.code) {
    throw new Error('必填参数不能为空！')
  }
  // 2、获取 Access token
  const accessTokenInfo = await getAccessToken(req.body.code)
  // 3、获取用户信息
  const userInfo = await getUserInfo(accessTokenInfo.access_token)
  // 4、在这步你可以将用户信息存入数据库中等其他操作，这里我直接返回了
  res.status(200).send(userInfo)
})

// 获取 access_token
async function getAccessToken(code) {
  // 官方文档： https://gitee.com/api/v5/oauth_doc#/list-item-2
  // 向gitee发送post请求，成功的话会，response.data里面有一个access_token
  try {
    const response = await axios({
      method: 'post',
      url: 'https://gitee.com/oauth/token',
      params: {
        grant_type: 'authorization_code',
        code,
        redirect_uri: giteeConfig.redirect_uri,
        client_id: giteeConfig.client_id,
        client_secret: giteeConfig.client_secret,
      },
    });

    if (!response.data?.access_token) {
      throw new Error('获取 access_token 失败！')
    }

    // response.data:{
    //   access_token: '3875ff7f7def72c6e05285895e4eb3f2',
    //   token_type: 'bearer',
    //   expires_in: 86400,
    //   refresh_token: '138ea028c08769fd798249bc297b095fa41ee4fdd9d5e55bbcef1a1b36db3338',
    //   scope: 'user_info projects pull_requests issues notes keys hook groups gists enterprises emails',
    //   created_at: 1725805674
    // }

    return response.data

  } catch (error) {
    throw new Error(error.response.data?.error_description)
  }
}


// 获取用户信息
async function getUserInfo(access_token) {
  //官方文档： https://gitee.com/api/v5/swagger#/getV5User
  const response = await axios({
    method: "get",
    url: 'https://gitee.com/api/v5/user',
    params: {
      access_token
    },
    headers: {
      'Content-Type': 'application/json',
      // Authorization: `Bearer ${access_token}`,
    },
  });

  console.log('response',response.data)

  if (!response.data?.id) throw new Error('获取用户信息失败！')

  /**
  response.data = {
    "id": 9534923,
    "login": "china-quanda",
    "name": "China-Quanda",
    "avatar_url": "https://foruda.gitee.com/avatar/1677178346321314348/9534923_china-quanda_1627964208.png",
    "url": "https://gitee.com/api/v5/users/china-quanda",
    "html_url": "https://gitee.com/china-quanda",
    "remark": "",
    "followers_url": "https://gitee.com/api/v5/users/china-quanda/followers",
    "following_url": "https://gitee.com/api/v5/users/china-quanda/following_url{/other_user}",
    "gists_url": "https://gitee.com/api/v5/users/china-quanda/gists{/gist_id}",
    "starred_url": "https://gitee.com/api/v5/users/china-quanda/starred{/owner}{/repo}",
    "subscriptions_url": "https://gitee.com/api/v5/users/china-quanda/subscriptions",
    "organizations_url": "https://gitee.com/api/v5/users/china-quanda/orgs",
    "repos_url": "https://gitee.com/api/v5/users/china-quanda/repos",
    "events_url": "https://gitee.com/api/v5/users/china-quanda/events{/privacy}",
    "received_events_url": "https://gitee.com/api/v5/users/china-quanda/received_events",
    "type": "User",
    "blog": null,
    "weibo": null,
    "bio": "时间过得很快，不记录很难回忆起来",
    "public_repos": 11,
    "public_gists": 0,
    "followers": 4,
    "following": 10,
    "stared": 241,
    "watched": 29,
    "created_at": "2021-08-03T12:11:18+08:00",
    "updated_at": "2024-09-08T14:52:57+08:00",
    "email": "864910436@qq.com"
  }
  */
  return response.data;
}

module.exports = router;
